package com.yuntongxun.kitsdk.customsize;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.yuntongxun.eckitsdk.R;
import com.yuntongxun.kitsdk.ui.ECSuperActivity;

import java.util.ArrayList;

/**
 * Created by David on 2015/10/15.
 */
public class AQContactActivity extends ECSuperActivity implements OnClickListener {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_aq_contact;
    }

    private static ArrayList<String> friends = new ArrayList<>();

    public static void setFriends(ArrayList<String> friend) {
        friends.clear();
        friends.addAll(friend);
    }

    public static ArrayList<String> getFriends() {
        return friends;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActivityState(savedInstanceState);
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction("cn.aq.addfriend.result");
        mFilter.addAction("cn.aq.removefriend.result");
        registerReceiver(myReceiver, mFilter);
    }

    boolean isFriend;
    String sessionId;

    /**
     * 读取聊天界面联系人会话参数信息
     */
    private void initActivityState(Bundle savedInstanceState) {
        Intent intent = getIntent();
        sessionId = intent.getStringExtra("sessionId");
        String username = intent.getStringExtra("username");
        Button button = (Button) findViewById(R.id.bt_add_friend);
        ((TextView) findViewById(R.id.nick_name)).setText(username);
        for (int i = 0; i < friends.size(); i++) {
            if (sessionId.equals(friends.get(i))) {
                button.setText("删除好友");
                isFriend = true;
            }
        }
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("what");
                if (isFriend) {
                    Intent intent = new Intent();
                    intent.setAction("cn.aq.removefriend");
                    intent.putExtra("id", sessionId);
                    sendBroadcast(intent);
                } else {
                    Intent intent = new Intent();
                    intent.setAction("cn.aq.addfriend");
                    intent.putExtra("id", sessionId);
                    sendBroadcast(intent);
                }
            }
        });
        getTopBarView().setTopBarToStatus(1, R.drawable.ytx_topbar_back_bt, null, "聊天设置", this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_left) {
            hideSoftKeyboard();
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println("myReceiver receive");
            String id = intent.getStringExtra("id");
            if (intent.getAction().equals("cn.aq.addfriend.result")) {
                friends.add(id);
            } else if (intent.getAction().equals("cn.aq.removefriend.result")) {
                friends.remove(id);
            }
            if (id.equals(sessionId)) {
                hideSoftKeyboard();
                finish();
            }
        }
    };
}